<template>
  <div>

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="queryParam.username" placeholder="输入账号模糊查询"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="queryParam.phone" placeholder="请输入手机号码查询"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item label="用户状态">
              <a-select v-model="queryParam.status" placeholder="请选择" style="width: 100px">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option value="1">正常</a-select-option>
                <a-select-option value="2">冻结</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="modelFormHandle()" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="exportHandle('用户信息')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDeleteHandle">
            <a-icon type="delete" />
            删除
          </a-menu-item>
          <a-menu-item key="2" @click="batchFrozen('2')">
            <a-icon type="lock" />
            冻结
          </a-menu-item>
          <a-menu-item key="3" @click="batchFrozen('1')">
            <a-icon type="unlock" />
            解冻
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
          ref="table"
          bordered
          size="middle"
          rowKey="id"
          :columns="columns"
          :dataSource="dataList"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          @change="tableChangeHandle">

        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <a-avatar shape="square" :src="getAvatarView(record.avatar)" icon="user" />
          </div>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="modelFormHandle(record.id)">编辑</a>

          <a-divider type="vertical" />

          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handleDetail(record)">详情</a>
              </a-menu-item>

              <a-menu-item>
                <a href="javascript:;" @click="handleChangePassword(record.username)">密码</a>
              </a-menu-item>

              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => deleteHandle(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>

              <a-menu-item v-if="record.status==1">
                <a-popconfirm title="确定冻结吗?" @confirm="() => handleFrozen(record.id,2,record.username)">
                  <a>冻结</a>
                </a-popconfirm>
              </a-menu-item>

              <a-menu-item v-if="record.status==2">
                <a-popconfirm title="确定解冻吗?" @confirm="() => handleFrozen(record.id,1,record.username)">
                  <a>解冻</a>
                </a-popconfirm>
              </a-menu-item>

            </a-menu>
          </a-dropdown>
        </span>


      </a-table>
    </div>
    <!-- table区域-end -->

    <model-form v-if="modelFormVisible" ref="modelForm" @ok="modalFormOk"></model-form>
    <user-update-password v-if="userUpdatePasswordVisible" ref="userUpdatePassword"></user-update-password>
  </div>
</template>

<script>
import listMixin from '@/mixins/list-mixin'
import modelForm from './model/user-form'
import userUpdatePassword from './model/user-update-password'

export default {
  name: "user",
  mixins: [listMixin],
  components: {
    modelForm,
    userUpdatePassword
  },
  data () {
    return {
      disableMixinCreated: true,
      userUpdatePasswordVisible: false,
      queryParam: {
        status: ''
      },
      url: {
        list: '/sys/user/page',
        delete: '/sys/user',
        export: '/sys/user/export',
        importExcelUrl: "sys/user/import",
      },
      columns: [
        /*{
          title: '#',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },*/
        {
          title: '用户账号',
          align: "center",
          dataIndex: 'username',
          sorter: true
        },
        {
          title: '用户姓名',
          align: "center",
          dataIndex: 'realName',
        },
        {
          title: '工号',
          align: "center",
          dataIndex: 'workNo',
        },
        {
          title: '邮箱',
          align: "center",
          dataIndex: 'email'
        },
        {
          title: '手机号码',
          align: "center",
          dataIndex: 'mobile'
        },
        {
          title: '性别',
          align: "center",
          dataIndex: 'gender',
          customRender: function (text) {
            if (text === 0) {
              return '男'
            } else {
              return '女'
            }
          }
        },
        {
          title: '部门',
          align: "center",
          dataIndex: 'deptName'
        },
        {
          title: '状态',
          align: "center",
          dataIndex: 'status',
          customRender: function (text) {
            if (text === 1) {
              return '正常'
            } else {
              return '冻结'
            }
          }
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createDate'
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: "center",
          width: 150,
          fixed: 'right'
        }
      ],
      labelCol: {
        xs: { span: 8 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 16 },
        sm: { span: 18 },
      },
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${ window._CONFIG['domianURL'] }/${ this.url.importExcelUrl }`;
    }
  },
  methods: {
    handleChangePassword(username) {
      this.userUpdatePasswordVisible = true
      this.$nextTick(() => {
        this.$refs.userUpdatePassword.dataForm.username = username
        this.$refs.userUpdatePassword.init()
      })
    }
  }
}
</script>

<style scoped>

</style>
